﻿using Autofac;
using NewsSevicer.Interface;
using NewsSevicer;
using System.ComponentModel;
using Microsoft.EntityFrameworkCore;
using NewsEntity.Models;
using Microsoft.Extensions.Configuration;
using Pomelo.EntityFrameworkCore.MySql.Storage;
namespace NewsWebsite.Configuration
{

    public static class AutofacConfig
    {
        public static void ConfigureContainer(ContainerBuilder builder)
        {
            // 注册服务
            builder.RegisterType<OnlineUserService>().As<IOnlineUserService>().InstancePerLifetimeScope();
            builder.RegisterType<AdminServer>().As<IAdminServer>().InstancePerLifetimeScope();
            builder.RegisterType<NewsServer>().As<INews>().InstancePerLifetimeScope();
            builder.RegisterType<TestServier>().As<ITest>().InstancePerLifetimeScope();


            // 注册仓储接口的实现（假设你有对应的实现类）


            // 注册 DbContext（建议从配置读取连接字符串）
            builder.Register(context =>
            {
                var configuration = context.Resolve<IConfiguration>();
                var connectionString = configuration.GetConnectionString("NewsDatabase");
                var optionsBuilder = new DbContextOptionsBuilder<NewsContext>();
                optionsBuilder.UseMySql(connectionString);
                return new NewsContext(optionsBuilder.Options);
            }).InstancePerLifetimeScope();
        }
    }
}
